'''
Created on 14.12.2012

@author: fre
'''
from game.player.alpha_beta_player.alpha_beta_framework.heuristic.Heuristic import \
    Heuristic
from game.datastructure.PentalathField import PentalathField
from game.datastructure.PlayerColor import PlayerColor
from game import GameLogic
import copy

class CenterHeuristic(Heuristic):
    '''
    classdocs
    '''
    def setDists(self, position, dist, field, REMOVE):
        ox,oy = position
        self.distanceGrid[oy][ox] = -dist
        if dist == 0:
            return
        actions = copy.deepcopy(GameLogic.actions)
        actions.remove(REMOVE)
        for action in actions:
            ax,ay = action
            nx,ny = ox,oy
            for i in xrange(dist):
                nx,ny = nx+ax, ny+ay
                if not GameLogic.inRange(field, (nx,ny)) or self.distanceGrid[ny][nx] != 0:
                    break
                self.distanceGrid[ny][nx] = -dist
        ax,ay = REMOVE
        nx,ny = ox+ax, oy+ay
        self.setDists((nx,ny), dist-1, field, REMOVE)
    
    
    def __init__(self, weighting):
        Heuristic.__init__(self, weighting)
        field = PentalathField()
        self.distanceGrid = field.field
        self.setDists((0,0), 4, field, [1,1])
        self.setDists((8,8), 4, field, [-1,-1])
        self.setDists((4,8), 4, field, [0,-1])
        self.setDists((8,4), 4, field, [-1,0])
            


    def heuristic(self, field, action, color, nextColor, depth):
        x,y = action
        value = 4+self.distanceGrid[y][x]
        return value
    
    